package ru.bmstu.vks.server.view.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class SaveFileDialog extends JDialog {
	private static final long serialVersionUID = 2841272791353871453L;
	private final JPanel contentPanel = new JPanel();
	private JFileChooser fileChooser;

	private File selectedFile = null;
	private JLabel accept;
	private JLabel cancel;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SaveFileDialog() {
		setModal(true);
		setIconImage(Toolkit.getDefaultToolkit().getImage(SaveFileDialog.class.getResource("/data/icon.png")));
		setTitle("\u0421\u043E\u0445\u0440\u0430\u043D\u0435\u043D\u0438\u0435 \u0444\u0430\u0439\u043B\u0430");
		setBounds(100, 100, 571, 375);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			fileChooser = new JFileChooser();
			contentPanel.add(fileChooser);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				{
					accept = new JLabel("");
					accept.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseClicked(MouseEvent e) {
							selectedFile = fileChooser.getSelectedFile();
							SaveFileDialog.this.setVisible(false);
						}
					});
					accept.setIcon(new ImageIcon(SaveFileDialog.class.getResource("/data/accept.png")));
					buttonPane.add(accept);
				}
			}
			{
				cancel = new JLabel("");
				cancel.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						selectedFile = null;
						SaveFileDialog.this.setVisible(false);
					}
				});
				cancel.setIcon(new ImageIcon(SaveFileDialog.class.getResource("/data/cancel.png")));
				buttonPane.add(cancel);
			}
		}
	}
	
	public void init(File file) {
		this.fileChooser.setSelectedFile(file);
	}
	
	public File getSelectedFile() {
		return selectedFile;
	}

}
